package kr.saeildamil.androidforstudy2.ch22;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Ball {
	int x, y, rad;
	int dx, dy;
	// 볼의 이동방향과 속도 x,y;
	int color;
	// 볼의 색깔;
	int count;
	// 볼이 벽에 부딪힌 횟수;
	
	public Ball(int x, int y){
		Random rnd = new Random();
		
		this.x = x;
		this.y = y;
		this.rad = rnd.nextInt(26) + 25;
		this.color = Color.rgb(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256));
		
		do{
			this.dx = rnd.nextInt(11) - 5;
			this.dy = rnd.nextInt(11) - 5;
		}while(this.dx == 0 || this.dy == 0);
	}
	
	/*static Ball Create(int x, int y, int rad){
		Random rnd = new Random();
		Ball newBall = new Ball();
		
		newBall.x = x;
		newBall.y = y;
		newBall.rad = rnd.nextInt(26) + 25; // 볼의 크기: 25~50;
		
		do{
			newBall.dx = rnd.nextInt() - 5;
			newBall.dy = rnd.nextInt() - 5;
		}while(newBall.dx==0 || newBall.dy==0);
		
		newBall.count = 0;
		newBall.color = Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
		
		return newBall;
		
	}*/
	
	void Move(int width, int height){
		// 움직임: 화면의 크기를 매개변수로 받음;
		
		x += dx;
		y += dy;
		
		if(x < rad || x > width-rad){
			dx *= -1; // 방향 전환;
			count++; // 벽에 부딪힌 수  +1;
		}
		
		if(y < rad || y > height - rad){
			dy *= -1;
			count ++;
		}
	}
	
	void Draw(Canvas canvas){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		for(int r=rad, alpha = 1; r > 4; r--, alpha += 5){
			paint.setColor(Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color)));
			canvas.drawCircle(x, y, r, paint);
		}
	}
	
	
	
}
